package com.example.stroebackstage.utils;
import java.sql.*;

public class LinkMySQL {
    public static Connection getConnection(){
        String URL = "jdbc:mysql://localhost:3308/mystore?characterEncoding=utf8&useSSL=true";
        String UserName = "root";
        String Password = "1234567";
        Connection conn = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver"); // 加载驱动
            //通过DriverManager类的getConnection方法指定三个参数,连接数据库对象
            conn = DriverManager.getConnection(URL, UserName, Password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }
    //释放资源
    public static void release(ResultSet resultSet, Statement statement, Connection connection){
        try {
            if (resultSet != null){
                resultSet.close();
            }
            if (statement != null){
                statement.close();
            }
            if (connection != null){
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    //释放资源
    public static void release(Statement statement, Connection connection) {
        try {
            if (statement != null){
                statement.close();
            }
            if (connection != null){
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

